/**
 * 
 */
package com.blog.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.domain.Blog;
import com.blog.domain.Category;
import com.blog.domain.Comment;
import com.blog.domain.User;

/**
 * @author cloris
 *
 */
@Controller
@RequestMapping(value = "/blog.do")
public class BlogController extends BaseAction{
	

	@RequestMapping(params = "blogdetails")
	@ResponseBody
	public void getBlogDetails(@ModelAttribute User usr,ServletRequest request, ServletResponse response){
		HttpServletRequest hrequest = (HttpServletRequest) request;
        HttpSession session = hrequest.getSession();
        String t = hrequest.getParameter("blogpage").toString();
        int page = 0;
        if(t!=null && !t.equals("")) {
        	page  = Integer.parseInt(t);
        }
		List<Blog> blogs= blogService.queryBlogs(page*20, 20);
		List<Category> categorys = blogService.queryCategorys();
		List<Comment> comments = blogService.queryComments(-1);
        session.setAttribute("categorys", categorys);
        session.setAttribute("blogs", blogs);
        session.setAttribute("comments", comments);
        hrequest.setAttribute("blogpage", page);
        try {
			hrequest.getRequestDispatcher("/views/blog/blogMenuShow.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(params = "pre_next")
	@ResponseBody
	public void getPreOrNextBlog(int id,ServletRequest request, ServletResponse response){
		HttpServletRequest hrequest = (HttpServletRequest) request;
        HttpSession session = hrequest.getSession();
		Blog blog= blogService.queryBlog(id);
        session.setAttribute("recentblog", blog);
		List<Comment> comments = blogService.queryComments(id);
        session.setAttribute("bcom", comments);
        try {
			hrequest.getRequestDispatcher("/views/home/homeMenuShow.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cate_blogs")
	@ResponseBody
	public void geBlogsByAlbumId(int cid,ServletRequest request, ServletResponse response){
		HttpServletRequest hrequest = (HttpServletRequest) request;
        HttpSession session = hrequest.getSession();
		List<Blog> blogs= blogService.queryBlogsByCateId(cid);
		session.setAttribute("blogs", blogs);
        try {
			hrequest.getRequestDispatcher("/views/blog/blogMenuShow.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(params = "save_com")
	public String saveComment(String comname, String comcon,Integer id,ModelMap modelMap,ServletRequest request, ServletResponse response){
		if(comname==null ||comname.equals("")||comcon==null ||comcon.equals("")){	
			modelMap.put("msg", "您的评论缺少评论人或者内容");
		}else{
			Comment com = new Comment();
			com.setBlogId(id);
			com.setContent(comcon);
			com.setUsername(comname);
			com.setCreatedTime(new Timestamp(System.currentTimeMillis()));
			blogService.saveComment(com);
		}
		homeSiderData(id,modelMap);    
		List<Comment> comments = blogService.queryComments(id);
		modelMap.put("bcom", comments);
        return "/views/home/homeMenuShow.jsp";		
	}
	@RequestMapping(params = "add_blog_category")
	@ResponseBody
	public void getCategory(@ModelAttribute Category cate,ServletRequest request, ServletResponse response){
		HttpServletRequest hrequest = (HttpServletRequest) request;
		String name=request.getParameter("name");
		if(name!=null){
		        try {
					name=new String(name.getBytes("ISO8859_1"),"UTF8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
		}
		cate.setName(name);
        int res = blogService.saveCategory(cate);
        try {
        	if(res>0) {
        		hrequest.setAttribute("msg", "创建博客种类成功");
        		hrequest.getRequestDispatcher("/views/blog/addCategory.jsp").forward(request, response);
        	}
        	else {
        		hrequest.setAttribute("msg", "创建博客种类失败");
        		hrequest.getRequestDispatcher("/views/hint.jsp").forward(request, response);}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(params = "category_list")
	public void getCategory(ServletRequest request, ServletResponse response) {
		HttpServletRequest hrequest = (HttpServletRequest) request;
		HttpSession session = hrequest.getSession();
		List<Category> cates = blogService.queryCategorys();
		session.setAttribute("categorys", cates);

		try {
			hrequest.getRequestDispatcher("views/blog/addBlog.jsp").forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(params = "saveBlog")
	public void saveBlog(@ModelAttribute Blog blog,ServletRequest request, ServletResponse response) {
		HttpServletRequest hrequest = (HttpServletRequest) request;		
		String title=request.getParameter("title");
		String content=request.getParameter("content");	
		String path = "views/blog/addBlog.jsp";
		if(title!=null&&!"".equals(title)&&content!=null&&!"".equals(content)){
		        try {
		        	title=new String(title.getBytes("ISO8859_1"),"UTF8");
		        	content=new String(content.getBytes("ISO8859_1"),"UTF8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
		        blog.setTitle(title);
				blog.setContent(content);
				int res = blogService.saveBlog(blog);
				if(res>0) {
					hrequest.setAttribute("msg", "博文保存成功");
					path = "views/hint.jsp";
				}
				else {
					hrequest.setAttribute("msg", "博文保存失败~~~");
				}
		}else{
			hrequest.setAttribute("msg", "标题或者内容不能为空");
		}
		
        
		try {
			hrequest.getRequestDispatcher(path).forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
